package com.example.ch2_4dlalogtoast;

import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.os.Bundle;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {

    private Button btnExit, btnGender,btnToast;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //获取界面上的控件
        getView();

        //注册监听器
        setListener();



    }

    private void setListener() {
        CustomOnClickListener listener = new CustomOnClickListener();

        btnExit.setOnClickListener(listener);

        //给性别选择按钮注册监听器
        btnGender.setOnClickListener(listener);

        btnToast.setOnClickListener(listener);

    }

    private void getView() {
        btnExit = findViewById(R.id.btn_exit);
        btnGender = findViewById(R.id.btn_gender);
        btnToast = findViewById(R.id.btn_toast);
    }




    //自定义点击事件监听器类
    class CustomOnClickListener implements View.OnClickListener{

        @Override
        public void onClick(View v) {
            switch (v.getId()){
                case R.id.btn_exit:
                    //使用对话框提醒
                    showAlertDialog();
                    break;
                case R.id.btn_gender:
                    showGenderDialog();
                    break;
                case R.id.btn_toast:
                    showToast();
                    break;
            }
        }
    }

    private void showToast() {
        //获取提示文本框(设置提示文本，显示时长)
        Toast.makeText(this,
                "提示文本信息",
                Toast.LENGTH_LONG).show();
/*
        //第二种创建对象的方法   很少用
        Toast toast = new Toast(this);
        toast.setGravity(
                //底部左对齐
                Gravity.BOTTOM|Gravity.LEFT,
                0,
                0
        );
        toast.setView(LayoutInflater.from(this)
                .inflate(
                        R.layout.custom_dialog_layout
                        ,null));
        toast.setDuration(Toast.LENGTH_SHORT);
        toast.show();
*/
    }

    private void showGenderDialog() {
        //加载自定义的对话框布局文件
        View dialogView = getLayoutInflater().inflate(
                R.layout.custom_dialog_layout,
                null
        );
        //获取自定义对话框界面上的radioGroup对象
        RadioGroup rgGender = dialogView.findViewById(R.id.rg_gender);

        //1.实例化对话框构造器对象
        AlertDialog.Builder builder = new AlertDialog.Builder(this);

        //2.设置属性
        builder.setCancelable(false)
                .setView(dialogView)  //设置对话框自定义的view
                .setPositiveButton("确定", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //日志显示性别
                        String gender = null;
                        switch (rgGender.getCheckedRadioButtonId()){
                            case R.id.rb_male:
                                gender = "男";
                                break;
                            case R.id.rb_female:
                                gender = "女";
                                break;
                            default:
                                gender = "未选则性别";
                                break;
                        }
                        Log.i("MainActivity",gender);
                        Toast.makeText(MainActivity.this,
                                gender,
                                Toast.LENGTH_LONG).show();
                    }
                });

        //3.创建对话框对象
        AlertDialog dialog = builder.create();
        //4.显示对话框
        dialog.show();
    }

    private void showAlertDialog() {
        //1.实例化对话框构造器对象
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        //2.设置对话框的属性
        builder.setCancelable(false)  //模式对话框（点击对话框外部不自动关闭对话框）
                .setTitle("退出应用提醒")    //设置对话框的标题
                .setMessage("确定要退出应用吗？")   //设置对话框提示文本信息
                .setPositiveButton("确定", new DialogInterface.OnClickListener() { //对话框上的确定按钮
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        //退出当前应用
                        MainActivity.this.finish();
                    }
                })
                .setNegativeButton("取消",null);//对话框上的取消按钮
        //3.使用构造器对象创建对话框对象
        AlertDialog dialog = builder.create();
        //4.显示对话框
        dialog.show();


    }


}












